Option Explicit On 
Option Strict On

Imports IP.Core.IPUserService

Imports IP.Core.IPData
Imports IP.Core.IPException
Imports IP.Core.IPCommon

Class f102_TuDien_DE
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()


        'Add any initialization after the InitializeComponent() call
        formatControls()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtMaTuDien As System.Windows.Forms.TextBox
    Friend WithEvents txtTenNgan As System.Windows.Forms.TextBox
    Friend WithEvents txtTen As System.Windows.Forms.TextBox
    Friend WithEvents m_cmd_ok As SIS.Controls.Button.SiSButton
    Friend WithEvents m_cmd_cancel As SIS.Controls.Button.SiSButton
    Friend WithEvents m_imagelist_DE As System.Windows.Forms.ImageList
    Friend WithEvents m_lbl_ma_tu_dien As System.Windows.Forms.Label
    Friend WithEvents m_lbl_ten_ngan As System.Windows.Forms.Label
    Friend WithEvents m_lbl_ghi_chu As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(f102_TuDien_DE))
        Me.m_lbl_ma_tu_dien = New System.Windows.Forms.Label
        Me.m_lbl_ten_ngan = New System.Windows.Forms.Label
        Me.m_lbl_ghi_chu = New System.Windows.Forms.Label
        Me.txtMaTuDien = New System.Windows.Forms.TextBox
        Me.txtTenNgan = New System.Windows.Forms.TextBox
        Me.txtTen = New System.Windows.Forms.TextBox
        Me.m_cmd_ok = New SIS.Controls.Button.SiSButton
        Me.m_imagelist_DE = New System.Windows.Forms.ImageList(Me.components)
        Me.m_cmd_cancel = New SIS.Controls.Button.SiSButton
        Me.SuspendLayout()
        '
        'm_lbl_ma_tu_dien
        '
        Me.m_lbl_ma_tu_dien.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.m_lbl_ma_tu_dien.Location = New System.Drawing.Point(-8, 4)
        Me.m_lbl_ma_tu_dien.Name = "m_lbl_ma_tu_dien"
        Me.m_lbl_ma_tu_dien.Size = New System.Drawing.Size(90, 23)
        Me.m_lbl_ma_tu_dien.TabIndex = 0
        Me.m_lbl_ma_tu_dien.Text = "Mã từ điển:"
        Me.m_lbl_ma_tu_dien.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'm_lbl_ten_ngan
        '
        Me.m_lbl_ten_ngan.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.m_lbl_ten_ngan.Location = New System.Drawing.Point(-26, 27)
        Me.m_lbl_ten_ngan.Name = "m_lbl_ten_ngan"
        Me.m_lbl_ten_ngan.Size = New System.Drawing.Size(108, 24)
        Me.m_lbl_ten_ngan.TabIndex = 1
        Me.m_lbl_ten_ngan.Text = "Tên ngắn:"
        Me.m_lbl_ten_ngan.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'm_lbl_ghi_chu
        '
        Me.m_lbl_ghi_chu.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.m_lbl_ghi_chu.Location = New System.Drawing.Point(-26, 92)
        Me.m_lbl_ghi_chu.Name = "m_lbl_ghi_chu"
        Me.m_lbl_ghi_chu.Size = New System.Drawing.Size(108, 24)
        Me.m_lbl_ghi_chu.TabIndex = 2
        Me.m_lbl_ghi_chu.Text = "Ghi chú:"
        Me.m_lbl_ghi_chu.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'txtMaTuDien
        '
        Me.txtMaTuDien.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtMaTuDien.Location = New System.Drawing.Point(88, 4)
        Me.txtMaTuDien.MaxLength = 50
        Me.txtMaTuDien.Name = "txtMaTuDien"
        Me.txtMaTuDien.Size = New System.Drawing.Size(128, 22)
        Me.txtMaTuDien.TabIndex = 3
        Me.txtMaTuDien.Text = ""
        '
        'txtTenNgan
        '
        Me.txtTenNgan.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTenNgan.Location = New System.Drawing.Point(88, 28)
        Me.txtTenNgan.MaxLength = 50
        Me.txtTenNgan.Name = "txtTenNgan"
        Me.txtTenNgan.Size = New System.Drawing.Size(336, 22)
        Me.txtTenNgan.TabIndex = 4
        Me.txtTenNgan.Text = ""
        '
        'txtTen
        '
        Me.txtTen.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTen.Location = New System.Drawing.Point(88, 52)
        Me.txtTen.MaxLength = 250
        Me.txtTen.Multiline = True
        Me.txtTen.Name = "txtTen"
        Me.txtTen.Size = New System.Drawing.Size(336, 104)
        Me.txtTen.TabIndex = 5
        Me.txtTen.Text = ""
        '
        'm_cmd_ok
        '
        Me.m_cmd_ok.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_cmd_ok.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.m_cmd_ok.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_cmd_ok.BtnStyle = SIS.Controls.Button.emunType.XPStyle.Default
        Me.m_cmd_ok.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_cmd_ok.ImageIndex = 0
        Me.m_cmd_ok.ImageList = Me.m_imagelist_DE
        Me.m_cmd_ok.Location = New System.Drawing.Point(246, 172)
        Me.m_cmd_ok.Name = "m_cmd_ok"
        Me.m_cmd_ok.Size = New System.Drawing.Size(88, 24)
        Me.m_cmd_ok.TabIndex = 17
        Me.m_cmd_ok.Text = "&Lưu"
        '
        'm_imagelist_DE
        '
        Me.m_imagelist_DE.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit
        Me.m_imagelist_DE.ImageSize = New System.Drawing.Size(16, 16)
        Me.m_imagelist_DE.ImageStream = CType(resources.GetObject("m_imagelist_DE.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.m_imagelist_DE.TransparentColor = System.Drawing.Color.Transparent
        '
        'm_cmd_cancel
        '
        Me.m_cmd_cancel.AdjustImageLocation = New System.Drawing.Point(0, 0)
        Me.m_cmd_cancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.m_cmd_cancel.BtnShape = SIS.Controls.Button.emunType.BtnShape.Rectangle
        Me.m_cmd_cancel.BtnStyle = SIS.Controls.Button.emunType.XPStyle.Default
        Me.m_cmd_cancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.m_cmd_cancel.ImageIndex = 2
        Me.m_cmd_cancel.ImageList = Me.m_imagelist_DE
        Me.m_cmd_cancel.Location = New System.Drawing.Point(338, 172)
        Me.m_cmd_cancel.Name = "m_cmd_cancel"
        Me.m_cmd_cancel.Size = New System.Drawing.Size(88, 24)
        Me.m_cmd_cancel.TabIndex = 16
        Me.m_cmd_cancel.Text = "&Bỏ"
        '
        'f102_TuDien_DE
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(426, 199)
        Me.Controls.Add(Me.m_cmd_cancel)
        Me.Controls.Add(Me.txtTen)
        Me.Controls.Add(Me.txtTenNgan)
        Me.Controls.Add(Me.txtMaTuDien)
        Me.Controls.Add(Me.m_lbl_ghi_chu)
        Me.Controls.Add(Me.m_lbl_ten_ngan)
        Me.Controls.Add(Me.m_lbl_ma_tu_dien)
        Me.Controls.Add(Me.m_cmd_ok)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "f102_TuDien_DE"
        Me.Text = "M102 - Cap nhat tu dien"
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Các thuộc tính riêng"
    Private m_FormMode As IPConstants.DataEntryFormMode
    Private m_USTuDien As US_CM_DM_TU_DIEN
    Private m_DialogResult As DialogResult
#End Region

#Region "Public interface"
    Public Function InsertObj(ByVal i_objTuDien As US_CM_DM_TU_DIEN) As DialogResult
        m_FormMode = IPConstants.DataEntryFormMode.InsertDataState
        m_USTuDien = i_objTuDien
        Me.ShowDialog()
        Return m_DialogResult
    End Function

    Public Function UpdateObj(ByVal i_objTuDien As US_CM_DM_TU_DIEN) As DialogResult
        m_FormMode = IPConstants.DataEntryFormMode.UpdateDataState
        m_USTuDien = i_objTuDien
        Me.ShowDialog()
        Return m_DialogResult
    End Function
#End Region

#Region "Private method"
    Private Sub formatControls()
        CControlFormat.setFormStyle(Me)
        CControlFormat.setLabelStyle(Me.m_lbl_ma_tu_dien, LabelStyle.Prompt_Info)
        CControlFormat.setLabelStyle(Me.m_lbl_ghi_chu, LabelStyle.Prompt_Info)
        CControlFormat.setLabelStyle(Me.m_lbl_ten_ngan, LabelStyle.Prompt_Info)

        Me.KeyPreview = True

    End Sub
    Private Function CheckValidate() As Boolean
        If Not CValidateTextBox.IsValid(txtMaTuDien, DataType.StringType, allowNull.NO) Then Return False
        If Not CValidateTextBox.IsValid(Me.txtTenNgan, DataType.StringType, allowNull.NO) Then Return False
        Return True
    End Function

    Private Sub Form2USObj(ByVal i_objUS As US_CM_DM_TU_DIEN)
        i_objUS.strMA_TU_DIEN = txtMaTuDien.Text
        i_objUS.strTEN = txtTen.Text
        i_objUS.strTEN_NGAN = txtTenNgan.Text
    End Sub

    Private Sub USObj2Form(ByVal i_objUS As US_CM_DM_TU_DIEN)
        txtMaTuDien.Text = i_objUS.strMA_TU_DIEN
        txtTenNgan.Text = i_objUS.strTEN_NGAN
        txtTen.Text = i_objUS.strTEN
    End Sub

#End Region


    Private Sub f101_TuDien_DE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_DialogResult = DialogResult.Cancel
        Select Case m_FormMode
            Case IPConstants.DataEntryFormMode.InsertDataState

            Case IPConstants.DataEntryFormMode.UpdateDataState
                Me.USObj2Form(m_USTuDien)
        End Select
    End Sub


    Private Sub execute_insert(ByVal i_us_tu_dien As US_CM_DM_TU_DIEN)
        Try
            i_us_tu_dien.open_connection()
            i_us_tu_dien.Insert()
            i_us_tu_dien.commit_close_connection()
        Catch v_e As Exception
            i_us_tu_dien.rollback_close_connection()
            Throw v_e
        End Try
    End Sub



    Private Sub f101_TuDien_DE_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Try
            Select Case e.KeyCode
                Case Keys.Escape
                    Me.Close()
            End Select
        Catch v_e As Exception
            CSystemLog_301.ExceptionHandle(v_e)
        End Try
    End Sub

    Private Sub m_cmd_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_cmd_ok.Click
        If Not Me.CheckValidate() Then
            Exit Sub
        End If

        Me.Form2USObj(m_USTuDien)
        Try
            Select Case m_FormMode
                Case IPConstants.DataEntryFormMode.InsertDataState
                    execute_insert(m_USTuDien)
                Case IPConstants.DataEntryFormMode.UpdateDataState
                    m_USTuDien.Update()
            End Select
            m_DialogResult = DialogResult.OK
            Me.Close()
        Catch v_e As Exception
            Dim v_ErrHandler As New CDBExceptionHandler(v_e, New CDBClientDBExceptionInterpret)
            v_ErrHandler.setErrorMsg(CDBExceptionHandler.DBErrorIndex.NoParentFound, "Không tìm thấy loại từ điển")
            v_ErrHandler.showErrorMessage()
        End Try
    End Sub

    Private Sub m_cmd_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_cmd_cancel.Click
        Me.Close()
    End Sub
End Class
